import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gift_listing/common/utils.dart';
import 'package:gift_listing/global.dart';
import 'package:gift_listing/manager/gift_manager.dart';
import 'package:gift_listing/pages/edit.dart';
import 'package:provider/provider.dart';

class GiftDetailPage extends StatefulWidget {
  GiftDetailPage({Key key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _GiftDetailPageState();
}

class _GiftDetailPageState extends State<GiftDetailPage> {
  Widget buildGiftList(_giftManager) {
    List<Widget> _children = [];
    List.generate(_giftManager.currentGift['gifts'].length, (index) {
      final gift = _giftManager.currentGift['gifts'][index];
      _children.add(Row(
        children: [
          Checkbox(
            value: gift['completed'] == 1,
            activeColor: Global.fontColor, //选中时的颜色
            onChanged: (value) {
              _giftManager.setGiftCompleted(index, value);
            },
          ),
          Expanded(
            flex: 2,
            child: Text(
              gift['gift_name'],
              style: TextStyle(
                decoration: gift['completed'] == 1
                    ? TextDecoration.lineThrough
                    : TextDecoration.none,
              ),
            ),
          ),
          SizedBox(width: 10),
          Expanded(
            flex: 1,
            child: Text.rich(TextSpan(children: [
              TextSpan(text: '给   '),
              TextSpan(
                text: gift['recipient'],
                style: TextStyle(fontWeight: FontWeight.w500),
              ),
            ])),
          ),
        ],
      ));
    });
    return Column(children: _children);
  }

  @override
  Widget build(BuildContext context) {
    GiftManager _giftManager = Provider.of<GiftManager>(context);

    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.delete),
            tooltip: '删除',
            onPressed: () {
              Utils.showConfirmDialog(
                context: context,
                content: '确认删除该清单？',
                confirmCallback: () async {
                  // ignore: await_only_futures
                  await _giftManager.delete(id: _giftManager.currentGift['id']);
                  Navigator.pop(context);
                },
                showCancelButton: true,
              );
            },
          ),
          IconButton(
            icon: Icon(Icons.edit),
            tooltip: '编辑',
            onPressed: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => GiftEditPage(),
                ),
              );
            },
          )
        ],
        title: Text(
          _giftManager.currentGift['title'],
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 15,
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ),
      body: Container(
        padding: EdgeInsets.all(10),
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(color: Global.backgroundColor),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              formTitle(title: '预计准备时间：'),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(width: 7),
                  Text(
                    _giftManager.currentGift['startDate'],
                    style: TextStyle(
                      color: Global.fontColor,
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                  SizedBox(width: 10),
                  Text(
                    '至',
                    style: TextStyle(
                      color: Global.fontColor,
                      fontSize: 11,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  SizedBox(width: 10),
                  Text(
                    _giftManager.currentGift['endDate'],
                    style: TextStyle(
                      color: Global.fontColor,
                      fontSize: 14,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                ],
              ),
              SizedBox(height: 15),
              formTitle(title: '备注'),
              SizedBox(height: 5),
              Container(
                padding: EdgeInsets.only(left: 7),
                child: Text(
                  _giftManager.currentGift['remark'],
                  style: TextStyle(
                    color: Global.fontColor,
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              SizedBox(height: 10),
              formTitle(
                title: '礼品清单',
                smTitle: '共${_giftManager.currentGift['gifts'].length}项',
              ),
              buildGiftList(_giftManager),
            ],
          ),
        ),
      ),
    );
  }
}

Widget formTitle({String title, String smTitle = ''}) {
  return Container(
    padding: EdgeInsets.fromLTRB(7, 20, 10, 7),
    child: Text.rich(
      TextSpan(
        children: [
          TextSpan(
            text: title,
            style: TextStyle(
              color: Global.fontColor,
              fontSize: 16,
              fontWeight: FontWeight.w600,
            ),
          ),
          TextSpan(
            text: '  $smTitle',
            style: TextStyle(
              color: Global.fontColor,
              fontSize: 11,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    ),
  );
}
